import pygame,script, projectile
import tile_tools as tt
import math as Math
from pygame.locals import *

class Player:
	def __init__(self,Px, Py):
		self.lock = False
		self.plist = []
		self.right = 0
		self.left = 0
		self.gap = 0
		self.g = 1
		self.jump_force = 0
		self.aimx = 0
		self.aimy = 0
		self.points = 0
		self.dirtid = 0
		self.inventory = "rock"
		self.v = 5
		self.x = Px
		self.y = Py
		self.prevX = self.x
		self.prevY = self.y
		self.MoveBits = [0,0,0,0]
		self.lft_set = []
		self.rht_set = []
		self.cur_frame = 0
		self.acid_num = 0
		self.is_grapple = False
		
		for i in range(0,10):
			tmpe = tt.get_tile_graphic(i,64,64,pygame.image.load("resources/psrt/lft.bmp"))
			self.lft_set.append(tmpe)
			tmpe = tt.get_tile_graphic(i,64,64,pygame.image.load("resources/psrt/rht.bmp"))
			self.rht_set.append(tmpe)
		self.idle_img = tt.get_tile_graphic(0,64,64,pygame.image.load("resources/psrt/idle.bmp"))

		
		#self.ply_surface = pygame.image.load("resources/char.bmp")
		self.w = 32
		self.h = 64
		#self.ply_surface.set_colorkey((255,0,255))
		self.e = self.v*self.g
		self.falling = False
		self.health = 100
		tmpscript = script.Script("resources/main.txt")
		self.hud_area = pygame.Surface((500, 32))
		self.hud_area.set_colorkey((255,0,255))
		self.health_txt = pygame.image.load(tmpscript.get_nvalue("HEALTH_TEXT","string","="))
		self.rock_icon = pygame.image.load(tmpscript.get_nvalue("ROCK_ICON","string","="))
		self.acid_icon = pygame.image.load(tmpscript.get_nvalue("ACID_ICON","string","="))
		self.fire_icon = pygame.image.load(tmpscript.get_nvalue("FIRE_ICON","string","="))
		self.shovel_icon = pygame.image.load(tmpscript.get_nvalue("SHOVEL_ICON","string","="))
		self.hook_icon = pygame.image.load(tmpscript.get_nvalue("HOOK_ICON","string","="))
		self.shoe_icon = pygame.image.load(tmpscript.get_nvalue("SHOE_ICON","string","="))
		self.points_txt = pygame.image.load(tmpscript.get_nvalue("POINTS_TEXT","string","="))
		self.health_txt.set_colorkey((255,0,255))
		self.hud_fnt = pygame.font.Font("resources/menu/xfiles.ttf",18)
		self.pnt_txt = pygame.Surface((100,32))
		
	def draw_hud_area(self):
		self.hud_area.fill((255,0,255))
		pygame.draw.rect(self.hud_area,(255,0,0),(80,0,200,15))
		pygame.draw.rect(self.hud_area,(0,255,0),(80,0,self.health*2,15))
		pygame.draw.rect(self.hud_area,(0,0,0),(80,0,200,15),5)
		if self.inventory == "rock":
			self.hud_area.blit(self.rock_icon,(300,0))
		elif self.inventory == "acid":
			self.hud_area.blit(self.acid_icon, (300,0))
		elif self.inventory == "fireball":
			self.hud_area.blit(self.fire_icon, (300,0))
		elif self.inventory == "shovel":
			self.hud_area.blit(self.shovel_icon, (300,0))
		elif self.inventory == "shoe":
			self.hud_area.blit(self.shoe_icon,(300,0))
		elif self.inventory == "hook":
			self.hud_area.blit(self.hook_icon,(300,0))
		self.hud_area.blit(self.health_txt,(0,0))
		self.hud_area.blit(self.points_txt,(360,0))
		self.pnt_txt = self.hud_fnt.render(str(self.points),True,(255,0,0))
		self.hud_area.blit(self.pnt_txt,(400,0))
		return self.hud_area
		
	def grapple(self):
		self.falling = False
		self.x = self.x + self.v*self.ganglex
		self.y = self.y + self.v*self.gangley
		
	def move_player(self,dx,dy,map):
		self.aimx = pygame.mouse.get_pos()[0] + 16
		self.aimy = pygame.mouse.get_pos()[1] + 16
		x = self.x - dx
		y = self.y + dy
		hyp = Math.sqrt(((x - self.aimx)**2) + ((y - self.aimy)**2))
		leg = abs(self.aimy - y)
		leg2 = abs(self.aimx - x)
		angley = Math.asin(leg/hyp)
		anglex = Math.acos(leg/hyp)
		if self.aimx > x and self.aimy < y:
			anglex = abs(anglex)
			angley = angley*-1
		elif self.aimx > x and self.aimy > y:
			anglex = abs(anglex)
			angley = abs(angley)
		elif self.aimx < x and self.aimy > y:
			anglex = anglex*-1
			angley = abs(angley)
		elif self.aimx < x and self.aimy < y:
			anglex = anglex*-1
			angley = angley*-1
		self.prevX = self.x
		self.prevY = self.y
		self.MoveBits = [0,0,0,0]
		if (pygame.key.get_pressed()[pygame.K_a]):
			self.x = self.x - self.v
			self.MoveBits[0] = 1
			self.cur_frame = self.cur_frame + 1
		elif (pygame.key.get_pressed()[pygame.K_d]):
			self.x = self.x + self.v
			self.MoveBits[1] = 1
			self.cur_frame = self.cur_frame + 1
		if (pygame.key.get_pressed() [pygame.K_1]):
			self.inventory = "rock"
		if (pygame.key.get_pressed() [pygame.K_2]):
			self.inventory = "acid"
		if (pygame.key.get_pressed() [pygame.K_3]):
			self.inventory = "fireball"
		if (pygame.key.get_pressed() [pygame.K_4]):
			self.inventory = "shovel"
		if (pygame.key.get_pressed() [pygame.K_5]):
			self.inventory = "shoe"
		if (pygame.key.get_pressed() [pygame.K_6]):
			self.inventory = "hook"
		# if (pygame.key.get_pressed()[pygame.K_DOWN]):
			# self.y = self.y + self.v
			# self.MoveBits[3] = 1
		if (map.poll == 1):
			if self.inventory == "shovel":
				if dx == 0 and dy == 0:
					db_x = abs(int(Math.floor((self.aimx+dx)/32)) )
					db_y = abs(int(Math.floor((self.aimy-dy)/32))) 
				elif dx <> 0 and dy == 0:
					db_x = abs(int(Math.ceil((self.aimx+dx)/32)) )
					db_y = abs(int(Math.ceil((self.aimy-dy)/32)) )
				elif dx == 0 and dy <> 0:
					db_x = abs(int(Math.ceil((self.aimx+dx)/32)) )+1
					db_y = abs(int(Math.ceil((self.aimy-dy)/32)) )
				elif dx <> 0 and dy <> 0:
					db_x = abs(int(Math.ceil((self.aimx+dx)/32)) )
					db_y = abs(int(Math.ceil((self.aimy-dy)/32)) )-1
				tmp_cell = map.maparray[db_y][db_x]	
				if map.tlist[tmp_cell][2] == "dirt":
					map.maparray[db_y][db_x] = -1
			if self.inventory == "hook":
				if dx == 0 and dy == 0:
					self.mb_x = abs(int(Math.floor((self.aimx+dx)/32)) )
					self.mb_y = abs(int(Math.floor((self.aimy-dy)/32))) 
				elif dx <> 0 and dy == 0:
					self.mb_x = abs(int(Math.ceil((self.aimx+dx)/32)) )
					self.mb_y = abs(int(Math.ceil((self.aimy-dy)/32)) )
				elif dx == 0 and dy <> 0:
					self.mb_x = abs(int(Math.ceil((self.aimx+dx)/32)) )+1
					self.mb_y = abs(int(Math.ceil((self.aimy-dy)/32)) )
				elif dx <> 0 and dy <> 0:
					self.mb_x = abs(int(Math.ceil((self.aimx+dx)/32)) )
					self.mb_y = abs(int(Math.ceil((self.aimy-dy)/32)) )-1
				#consec_n = (self.mb_y * map.reference.get_width()) - (map.reference.get_width() - self.mb_x)
				tmp_cell = map.maparray[self.mb_y][self.mb_x]
				if map.tlist[tmp_cell][2] == "hook":
					self.gx = (self.aimx+dx)
					self.gy = (self.aimy-dy)
					delta_x = abs(x - (self.aimx))
					delta_y = abs(y - (self.aimy))+64
					dist = Math.sqrt((delta_x*delta_x) + (delta_y*delta_y))
					self.ganglex = Math.acos( delta_y/dist )
					self.gangley = Math.asin( delta_y/dist )
					if self.aimx > x and self.aimy < y:
						self.ganglex = abs(self.ganglex)
						self.gangley = self.gangley*-1
					elif self.aimx > x and self.aimy > y:
						self.ganglex = abs(self.ganglex)
						self.gangley = abs(self.gangley)
					elif self.aimx < x and self.aimy > y:
						self.ganglex = self.ganglex*-1
						self.gangley = abs(self.gangley)
					elif self.aimx < x and self.aimy < y:
						self.ganglex = self.ganglex*-1
						self.gangley = self.gangley*-1
					self.is_grapple = True
			if len(self.plist) <= 2:
				if self.inventory == "rock" or self.inventory == "acid":
					tmpp = projectile.Projectile(self.inventory,20,"Player",False,50,20*anglex,20*angley,self.x,self.y)
					self.plist.append(tmpp)
				elif self.inventory == "fireball":
					angle_int = 0.2
					for i in range(1,4):
						tmpp = projectile.Projectile(self.inventory,20,"Player",False,50,20*angle_int*i,20*angle_int*(4-i),self.x,self.y)
						self.plist.append(tmpp)
					for i in range(1,4):
						tmpp = projectile.Projectile(self.inventory,20,"Player",False,50,20*angle_int*i,20*-angle_int*(4-i),self.x,self.y)
						self.plist.append(tmpp)
					for i in range(1,4):
						tmpp = projectile.Projectile(self.inventory,20,"Player",False,50,20*-angle_int*i,20*angle_int*(4-i),self.x,self.y)
						self.plist.append(tmpp)
					for i in range(1,4):
						tmpp = projectile.Projectile(self.inventory,20,"Player",False,50,20*-angle_int*i,20*-angle_int*(4-i),self.x,self.y)
						self.plist.append(tmpp)
		if self.is_grapple == True:
			B1 = pygame.Rect((self.x,self.y),(32,54))
			B2 = pygame.Rect((self.gx,self.gy),(32,32))
			if B1.colliderect(B2):
				self.is_grapple = False
			self.grapple()
		if self.jump_force >= 1:
			self.y = self.y - (self.v + self.jump_force)
			self.jump_force = self.jump_force - 2
		if (pygame.key.get_pressed()[pygame.K_w]) and self.falling == False:
			self.jump_force = 20
		if self.falling == True:
			self.MoveBits[2] = 1
			self.y = self.y + self.v*self.g
			self.g = self.g + 0.2
		else:
			self.g = 1
		if self.cur_frame == 10:
			self.cur_frame = 0
	
	def heal(self, h):
		self.health = self.health + h
		if self.health > 100:
			self.health = 100
	
	def restart(self,x,y):
		self.x = x
		self.y = y
		self.health = 100
	
	def do_damage(self,d):
		self.health = self.health - d
	
	def is_dead(self):
		if self.health > 0:
			return False
		else:
			return True
	
	def check_col2(self,map,mw,mh,pw,ph):
		c = 0
		cup = 0
		cdown = 0
		cleft = 0
		cright = 0
		apbot = 0
		aptop = 0
		tmpi = 0
		tmpj = 0
		slip = False
		onground = False
		r = [0,0]
		
		for i in range(0,map.bnum):
			B1 = pygame.Rect((map.blist[i].x,map.blist[i].y,32,32))
			B2 = pygame.Rect((self.x,self.y,pw,ph))
			if B1.colliderect(B2):
				r = [4,i]
		
		for proj in self.plist:
			if proj.move() == 0:
				self.plist.remove(proj)
			if proj.check_collision(map,self) == 1:
				self.plist.remove(proj)
		xrange = int(Math.floor(map.dx/32))
		yrange = abs(int(Math.floor(map.dy/32)))
		for i in range(yrange,yrange+20):
			for j in range(xrange, xrange+26):
				if i >= mh or j >= mw:
						break;
				if map.maparray[i][j] is not -1:
					b1top = pygame.Rect((self.x,self.y),(pw,1))
					b1bot = pygame.Rect((self.x,self.y+ph),(pw,1))
					b1left = pygame.Rect((self.x,self.y),(1,ph))
					b1 = pygame.Rect((self.x, self.y),(pw,ph))
					b2 = pygame.Rect((j*32,i*32),(32,32))
					trans = False
					if b1.colliderect(b2):
						tmp_list = map.tlist[map.maparray[i][j]]
						if tmp_list[2] == "spike" and self.inventory <> "shoe":
							self.do_damage(1)
						if tmp_list[2] == "sign":
							r = [2,i,j]
							trans = True
						if tmp_list[2] == "health":
							r = [3,i,j]
							trans = True
						if tmp_list[2] == "jar":
							r = [5,i,j]
						if tmp_list[2] == "hook":
							r = [6,i,j]
							trans = True
					if trans == False:
						if b1bot.colliderect(b2) and self.falling == True:
							slip = True
							if((j*32+32) - self.x) < ((self.y - i*32) + ph) or ((self.x - j*32) + pw) < ((self.y - i*32) + ph):
								onground = False
							else:
								onground = True
								self.y = (i*32 - ph)
						#B1 y->x
						b1tl = pygame.Rect((self.x,self.y),(pw/2,ph/2))
						#B1 y->x+w
						b1tr = pygame.Rect((self.x+pw/2,self.y),(pw/2,ph/2))
						#B1 y+h->x
						b1bl = pygame.Rect((self.x,self.y+ph/2),(pw/2,ph/2))
						#B1 y+h->x+w
						b1br = pygame.Rect((self.x+pw/2, self.y+ph/2),(pw/2,ph/2))
						#B2 y->x
						b2tl = pygame.Rect((j*32,i*32),(32/2,32/2))
						#B2 y->x+w
						b2tr = pygame.Rect((j*32+16,i*32),(32/2,32/2))
						#B2 y+h->x
						b2bl = pygame.Rect((j*32,i*32+16),(32/2,32/2))
						#B2 y+h->x+w
						b2br = pygame.Rect((j*32+16, i*32+16),(32/2,32/2))
					
						if b1bot.colliderect(b2):
							aptop = 1
						
						if b1br.colliderect(b2tr) and b1bl.colliderect(b2tl):
							cup = 1
							tmpj = i
						if b1tr.colliderect(b2br) or b1tl.colliderect(b2bl):
							cdown = 1
							tmpj = i
							tmpi = j
						if b1bl.colliderect(b2tr) or b1tl.colliderect(b2br):
							tmpi = j
							tmpj = i
							cright = 1
						if b1br.colliderect(b2tl) or b1tr.colliderect(b2bl):
							tmpi = j
							tmpj = i
							cleft = 1
						if  b1bl.colliderect(b2tr):
							jump_force = 0
							if ((j*32+32) - self.x) < ((self.y - i*32) + ph):
								onground = False
								cright = 1
								#cup = 0
							else:
								#onground = True
								cup = 1
								cright = 0
						if b1br.colliderect(b2tl):
							if ((self.x - j*32) + pw) <= ((self.y - i*32) + ph):
								cleft = 1
								#cup = 0
							else:
								cleft = 0
								cup = 1
						if b1tr.colliderect(b2bl):
							if ((self.x - tmpi*32) + pw) < ((tmpj*32+32) - self.y):
								cleft = 1
							else:
								cleft = 0
								cbot = 1
						if b1tr.colliderect(b2br):
							if ((j*32+32) - self.x) < ((self.y - i*32) + ph):
								onground = False
								cright = 1
								#cup = 0
							else:
								#onground = True
								cbot = 1
								cright = 0
				c = c + 1
		#onground = False
		if cup == 1 or (cup == 1 and (cleft == 1 or cright == 1)):
			self.y -= (self.y - tmpj*32) + ph
			#onground = True
		if cdown == 1:
			self.y += (tmpj*32+32) - self.y
		elif cleft == 1:
			self.x -=  (self.x - tmpi*32) + pw
		elif cright == 1:
			self.x += ((tmpi)*32+32) - self.x
		
		if onground == True:
			self.falling = False
		else:
			self.falling = True
		return r

	
	def draw(self):
		ply_surface = self.idle_img
		if self.MoveBits[0] == 1:
			ply_surface = self.lft_set[self.cur_frame]
		elif self.MoveBits[1] == 1:
			ply_surface = self.rht_set[self.cur_frame]
		return ply_surface